'use strict';

angular.module('User').controller('ChangePasswordController', ['$scope', 'User', 'ExtDialog', 
    function ($scope, User, ExtDialog) {
        $scope.data = {
            oldpassword: '',
            newpassword: '',
            verifypassword: ''
        };

        $scope.changePassword = function () {
            if ($scope.data.newpassword === '') {
                ExtDialog.tip('请输入旧密码');
                return;
            }
            if ($scope.data.newpassword === '') {
                ExtDialog.tip('请输入新密码');
                return;
            }
            if ($scope.data.verifypassword === '') {
                ExtDialog.tip('请再次输入新密码');
                return;
            }
            if ($scope.data.newpassword !== $scope.data.verifypassword) {
                ExtDialog.tip('两次输入的密码不一样，请重新输入');
                return;
            }
            ExtDialog.showLoading();
            User.changePassword(
                $scope.data.oldpassword, 
                $scope.data.newpassword, 
                $scope.data.verifypassword
            ).then(function () {
                ExtDialog.alert('密码修改成功，已用新密码登录');
            }).finally(function () {
                ExtDialog.hideLoading();
            });
        };
    }
]);
